package cn.sciento.farm.devicemanager.common.util.qiniu;

import cn.sciento.farm.devicemanager.common.util.PropertiesFileUtil;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Recorder;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.persistent.FileRecorder;
import com.qiniu.util.Auth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author wumu
 * @version V1.0
 * @date 5/10/18
 * @time 8:24 PM
 */
public class QiniuUploadUtil {

    private static Logger logger = LoggerFactory.getLogger(QiniuUploadUtil.class);

    //设置好账号的ACCESS_KEY和SECRET_KEY
    private static String accessKey = PropertiesFileUtil.getInstance(QiniuConstant.CONFIG_PATH).get(QiniuConstant.SERVER_ACCESS);
    private static String secretKey = PropertiesFileUtil.getInstance(QiniuConstant.CONFIG_PATH).get(QiniuConstant.SERVER_SECRET);
    private static String bucketName = PropertiesFileUtil.getInstance(QiniuConstant.CONFIG_PATH).get(QiniuConstant.SERVER_BUCKET);
    private static String serverUrl = PropertiesFileUtil.getInstance(QiniuConstant.CONFIG_PATH).get(QiniuConstant.SERVER_URL);


    private static Zone z = Zone.autoZone();
    private static Configuration c = new Configuration(z);

    //创建上传对象
    private static UploadManager uploadManager = new UploadManager(c);

    private static final long ONE_DAY = 86400L * 7;

    public static String getToken() {
        return getToken(bucketName, ONE_DAY);

    }

    public static String getToken(long expires) {
        return getToken(bucketName, expires);
    }

    public static String getToken(String bucketName) {
        return getToken(bucketName, ONE_DAY);
    }

    public static String getToken(String bucketName, Long expires) {
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucketName, null, expires, null);
        return upToken;
    }





    public static QiniuResult uploadByByte(byte[] bte) {
        return uploadByByte(bte, null);
    }

    public static QiniuResult uploadByByte(byte[] bte, String key) {
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucketName);
        try {
            Response response = uploadManager.put(bte, key, upToken);
            //解析上传成功的结果
            logger.info("qiniu Response body=> " + response.bodyString());
            return QiniuResult.forBoject(response);
        } catch (QiniuException e) {
            e.getStackTrace();
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public static QiniuResult uploadByPath(String path, String key) {
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucketName);
        try {
            Response response = uploadManager.put(path, key, upToken);
            //解析上传成功的结果
            logger.info("qiniu Response body=> " + response.bodyString());
            return QiniuResult.forBoject(response);
        } catch (QiniuException e) {
            e.getStackTrace();
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public static QiniuResult uploadByPath(String path) {
        return uploadByPath(path, null);
    }


    public static QiniuResult uploadByInputStream(InputStream inp) throws IOException {
        return uploadByInputStream(inp, null);
    }

    public static QiniuResult uploadByInputStream(InputStream inp, String key) throws IOException {
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucketName);
        try {
            Response response = uploadManager.put(inp, key, upToken, null, null);
            //解析上传成功的结果
            logger.info("qiniu Response body=> " + response.bodyString());
            return QiniuResult.forBoject(response);
        } catch (QiniuException e) {
            e.getStackTrace();
            logger.error(e.getMessage(), e);
        }finally {
            inp.close();
        }
        return null;
    }

    public QiniuResult uploadRecorder(String path, String recordPath, String key) throws IOException {
        //设置断点记录文件保存在指定文件夹或的File对象
        if (recordPath == null || recordPath.isEmpty()) {
            throw new NullPointerException("参数不正确");
        }
        //实例化recorder对象
        Recorder recorder = new FileRecorder(recordPath);
        //实例化上传对象，并且传入一个recorder对象
        UploadManager uploadManager = new UploadManager(c, recorder);

        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucketName);
        try {
            //调用put方法上传
            Response res = uploadManager.put(path, key, upToken);
            //打印返回的信息
            logger.info(res.bodyString());
            return QiniuResult.forBoject(res);
        } catch (QiniuException e) {
            e.getStackTrace();
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public static QiniuResult uploadRewrite(byte[] bte, String key) {
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucketName, key);
        try {
            Response response = uploadManager.put(bte, key, upToken);
            //解析上传成功的结果
            logger.info("qiniu Response body=> " + response.bodyString());
            return QiniuResult.forBoject(response);
        } catch (QiniuException e) {
            e.getStackTrace();
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public static QiniuResult uploadRewrite(String path, String key) {
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucketName, key);
        try {
            Response response = uploadManager.put(path, key, upToken);
            //解析上传成功的结果
            logger.info("qiniu Response body=> " + response.bodyString());
            return QiniuResult.forBoject(response);
        } catch (QiniuException e) {
            e.getStackTrace();
            logger.error(e.getMessage(), e);
        }
        return null;
    }


}
